Spring Framework:4.3.14.RELEASE
Framework Modules
创建一个简单的JavaBean:HelloWorld
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello() {
System.out.println("Hello " + name);
}
}
创建一个简单的 applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置 Bean-->
<bean id="helloWord" class="com.example.spring.HelloWorld">
<property name="name" value="Example"></property>
</bean>
</beans>
创建一个简单的使用示例
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWordTest02 {
public static void main(String[] args) {
// 创建 Spring IOC 容器
ApplicationContext applicationContext =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 从 IOC 容器中获取 Bean 实例
HelloWorld helloWorld = applicationContext.getBean("helloWorld", HelloWorld.class);
// 调用 setName 方法
helloWorld.setName("Example");
// 调用 hello 方法
helloWorld.hello();
}
}
代码请参考example-01:
lxmuse-spring-example-01